{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回查看“./目录0/目录1”文件夹下有哪些文件和目录\n",
    "# . 表示当前目录、.. 表示上一级目录\n",
    "os.listdir(\"./目录0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回查看“./目录0/目录1”文件夹下有哪些文件和文件夹\n",
    "os.listdir( \"./目录0/目录1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断路径是否为文件\n",
    "os.path.isfile(\"./目录0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 判断路径是否为文件夹\n",
    "os.path.isdir(\"./目录0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实现遍历2级文件夹\n",
    "# 遍历循环这个文件夹\n",
    "for fd in os.listdir(\"./目录0\"):\n",
    "    # 构建全路径\n",
    "    fd_path = \"./目录0/\" + fd\n",
    "    print(\"全路径：\", fd_path)\n",
    "    # 判断全路径是否为文件夹，如果是，再遍历循环这个文件夹\n",
    "    if os.path.isdir(fd_path):\n",
    "        for fd_1 in os.listdir(fd_path):\n",
    "            print(fd_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 同理可以实现3、4、5。。。级的文件夹，但显然不是好的办法\n",
    "# 怎么实现自动遍历的文件夹所有的文件？\n",
    "def show_dir(dir_path):\n",
    "    for fd in os.listdir(dir_path):\n",
    "        # 构建全路径\n",
    "        fd_path = dir_path + \"/\" + fd\n",
    "        print(\"全路径：\", fd_path)\n",
    "        if os.path.isdir(fd_path):\n",
    "            # 重点在这里，函数里面再次使用这个函数\n",
    "            show_dir(fd_path) # 我们称为“递归函数”\n",
    "\n",
    "# . 表示当前目录\n",
    "show_dir(\"./\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实现取 word 文件\n",
    "def show_dir(dir_path):\n",
    "    for fd in os.listdir(dir_path):\n",
    "        # 构建全路径\n",
    "        fd_path = dir_path + \"/\" + fd\n",
    "        if fd.endswith(\"docx\"):\n",
    "            print(\"word文件：\", fd_path)\n",
    "        if os.path.isdir(fd_path):\n",
    "            # 重点在这里，函数里面再次使用这个函数\n",
    "            show_dir(fd_path) # 我们称为“递归函数”\n",
    "\n",
    "# . 表示当前目录、.. 表示上一级目录\n",
    "show_dir(\"../\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_docx_files(dir_path):\n",
    "    docx_files = []\n",
    "    for fd in os.listdir(dir_path):\n",
    "        # 构建全路径\n",
    "        fd_path = dir_path + \"/\" + fd\n",
    "        if fd.endswith(\"docx\"):\n",
    "            docx_files.append(fd_path)\n",
    "        if os.path.isdir(fd_path):\n",
    "            # 重点在这里，函数里面再次使用这个函数\n",
    "            get_docx_files(fd_path) # 我们称为“递归函数”\n",
    "    return docx_files\n",
    "    \n",
    "print(get_docx_files(\"../\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把 docx_files 列表拿到函数外面就可以了\n",
    "docx_files = []\n",
    "def get_docx_files(dir_path):\n",
    "    for fd in os.listdir(dir_path):\n",
    "        # 构建全路径\n",
    "        fd_path = dir_path + \"/\" + fd\n",
    "        if fd.endswith(\"docx\"):\n",
    "            docx_files.append(fd_path)\n",
    "        if os.path.isdir(fd_path):\n",
    "            # 重点在这里，函数里面再次使用这个函数\n",
    "            get_docx_files(fd_path) # 我们称为“递归函数”\n",
    "    return docx_files\n",
    "    \n",
    "print(get_docx_files(\"../\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 变量的作用域\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 因为全局变量也有一些缺点（以后再讲解）\n",
    "# 如果我们不希望使用全局变量，以下写法会比较合适\n",
    "def get_docx_files(dir_path, docx_files=[]):\n",
    "    for fd in os.listdir(dir_path):\n",
    "        # 构建全路径\n",
    "        fd_path = dir_path + \"/\" + fd\n",
    "        if fd.endswith(\"docx\"):\n",
    "            docx_files.append(fd_path)\n",
    "        if os.path.isdir(fd_path):\n",
    "            # 重点在这里，函数里面再次使用这个函数\n",
    "            get_docx_files(fd_path, docx_files) # 我们称为“递归函数”\n",
    "    return docx_files\n",
    "\n",
    "print(get_docx_files(\"../\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_files(dir_path, file_ext_name, files=[]):\n",
    "    for fd in os.listdir(dir_path):\n",
    "        # 构建全路径\n",
    "        fd_path = dir_path + \"/\" + fd\n",
    "        if fd.endswith(file_ext_name):\n",
    "            files.append(fd_path)\n",
    "        if os.path.isdir(fd_path):\n",
    "            # 重点在这里，函数里面再次使用这个函数\n",
    "            get_files(fd_path, file_ext_name, files) # 我们称为“递归函数”\n",
    "    return files\n",
    "\n",
    "print(get_files(\"../\",\"docx\"))\n",
    "# print(get_files(\"../\",\"jpg\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_files(dir_path, ext_names, files=[]):\n",
    "    for fd in os.listdir(dir_path):\n",
    "        # 构建全路径\n",
    "        fd_path = dir_path + \"/\" + fd\n",
    "        for ext_name in ext_names:\n",
    "            if fd.endswith(ext_name):\n",
    "                files.append(fd_path)\n",
    "        if os.path.isdir(fd_path):\n",
    "            # 重点在这里，函数里面再次使用这个函数\n",
    "            get_files(fd_path, ext_names, files) # 我们称为“递归函数”\n",
    "    return files\n",
    "\n",
    "print(get_files(\"../\",[\"docx\",\"jpg\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
